package mypetstore.web.servlet;

import mypetstore.domain.Item;
import mypetstore.domain.Product;
import mypetstore.persistence.ProductDao;
import mypetstore.persistence.impl.ProductDaoImpl;
import mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class ProductFormServlet extends HttpServlet {
    private CatalogService catalogService;
    private static final String PRODUCT_FORM ="/WEB-INF/jsp/catalog/product.jsp" ;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String productId=req.getParameter("productId");
        catalogService=new CatalogService();
        Product product=catalogService.getProduct(productId);
        List<Item> itemList=catalogService.getItemListByProduct(productId);
        HttpSession session=req.getSession();
        session.setAttribute("product",product);
        session.setAttribute("itemList",itemList);
        req.getRequestDispatcher(PRODUCT_FORM).forward(req,resp);
    }


//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        HttpSession session = req.getSession();
//
//        String keyword =session.getAttribute("keyword",keyword );
//        catalogService=new CatalogService();
//        List<Product> productList = catalogService.searchProductList(keyword);
//        req.getRequestDispatcher(PRODUCT_FORM).forward(req,resp);
//
//    }
}
